//
//  ZJLocation.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/6/21.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJLocation.h"
#import <CoreLocation/CoreLocation.h> // 定位
#import "YYModel.h"
#import <TencentLBS/TencentLBS.h>
@interface ZJLocation ()<CLLocationManagerDelegate,TencentLBSLocationManagerDelegate>
/**    */
@property (strong, nonatomic) TencentLBSLocationManager  *tencentLocationManager;

/** 定位 */
//@property (nonatomic, strong) CLLocationManager *locationManager;
@end
@implementation ZJLocation


+ (ZJLocation *)sharedLocation{
    static ZJLocation *location = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
           location = [ZJLocation new];
    });
    return location;
}

/** 进行定位 */
- (void)getLocationSuccess:(MyBlock)success{
    self.block = success;
    [self locate];
}

#pragma mark - 定位
/** 定位 */
- (void)locate {
//    if([CLLocationManager locationServicesEnabled]){
    
//        if(!_locationManager){
//            
//            self.locationManager = [[CLLocationManager alloc] init];
//            
//            if([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){
//                [self.locationManager requestWhenInUseAuthorization];
//                [self.locationManager requestAlwaysAuthorization];
//                
//            }
//            //设置代理
//            [self.locationManager setDelegate:self];
//            //设置定位精度
//            [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
//            //设置距离筛选
//            [self.locationManager setDistanceFilter:100];
//            //开始定位
//            [self.locationManager startUpdatingLocation];
//            //设置开始识别方向
//            [self.locationManager startUpdatingHeading];
//        }else{
//            //开始定位
//            [self.locationManager startUpdatingLocation];
//            //设置开始识别方向
//            [self.locationManager startUpdatingHeading];
//        }
//    }else{
//        
//    }
    
    
    
    if (!self.tencentLocationManager) {
        self.tencentLocationManager = [[TencentLBSLocationManager alloc] init];
        
        [self.tencentLocationManager setDelegate:self];
        
        [self.tencentLocationManager setApiKey:@"W2YBZ-UZLC4-6KDUF-DVCKK-YXW76-3LBND"];
        
        [self.tencentLocationManager setPausesLocationUpdatesAutomatically:NO];
        
        // 需要后台定位的话，可以设置此属性为YES。
        [self.tencentLocationManager setAllowsBackgroundLocationUpdates:NO];
        
        // 如果需要POI信息的话，根据所需要的级别来设定，定位结果将会根据设定的POI级别来返回，如：
        [self.tencentLocationManager setRequestLevel:TencentLBSRequestLevelName];
        
        // 申请的定位权限，得和在info.list申请的权限对应才有效
        CLAuthorizationStatus authorizationStatus = [CLLocationManager authorizationStatus];
        if (authorizationStatus == kCLAuthorizationStatusNotDetermined) {
            [self.tencentLocationManager requestWhenInUseAuthorization];
        }
        [self.tencentLocationManager startUpdatingLocation];
    }else{
        [self.tencentLocationManager startUpdatingLocation];
//        [self.tencentLocationManager startUpdatingHeading];
    
    }
}
#pragma mark - TencentLBSLocationManagerDelegate
- (void)tencentLBSLocationManager:(TencentLBSLocationManager *)manager
                didUpdateLocation:(TencentLBSLocation *)location {
    [self.tencentLocationManager stopUpdatingLocation];
    [self reverseTenGeocoder:location];

}
- (void)tencentLBSLocationManager:(TencentLBSLocationManager *)manager
                 didFailWithError:(NSError *)error{
    //定位失败
}
//反地理编码
- (void)reverseTenGeocoder:(TencentLBSLocation *)Location{
    __weak typeof(self) weakSelf = self;
    CLGeocoder* geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:Location.location completionHandler:^(NSArray *placemarks, NSError *error) {
        if(error || placemarks.count == 0){
            NSLog(@"error = %@",error);
        }else{
            CLPlacemark* placemark = placemarks.firstObject;
            NSMutableDictionary *dict = [NSMutableDictionary dictionary];
            dict = [[placemark addressDictionary] mutableCopy];
            
            NSDictionary *location = @{@"longitude":[NSString stringWithFormat:@"%f",placemark.location.coordinate.longitude],@"latitude":[NSString stringWithFormat:@"%f",placemark.location.coordinate.latitude]};
            [dict setObject:location forKey:@"location"];
            
            if (weakSelf.block) {
                weakSelf.block(dict);
            }
            
        }
    }];
}



//#pragma mark - CLLocationManangerDelegate
////定位成功以后调用
//- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
//    
//    [self.locationManager stopUpdatingLocation];
//    CLLocation* location = locations.lastObject;
//    [self reverseGeocoder:location];
//}
//- (void)locationManager:(CLLocationManager *)manager
//       didFailWithError:(NSError *)error{
//    //定位失败
//}

//#pragma mark Geocoder
////反地理编码
//- (void)reverseGeocoder:(CLLocation *)currentLocation{
//    __weak typeof(self) weakSelf = self;
//    CLGeocoder* geocoder = [[CLGeocoder alloc] init];
//    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
//        if(error || placemarks.count == 0){
//            NSLog(@"error = %@",error);
//        }else{
//            CLPlacemark* placemark = placemarks.firstObject;
//            NSMutableDictionary *dict = [NSMutableDictionary dictionary];
//            dict = [[placemark addressDictionary] mutableCopy];
//            
//            NSDictionary *location = @{@"longitude":[NSString stringWithFormat:@"%f",placemark.location.coordinate.longitude],@"latitude":[NSString stringWithFormat:@"%f",placemark.location.coordinate.latitude]};
//            [dict setObject:location forKey:@"location"];
//            
//            if (weakSelf.block) {
//                weakSelf.block(dict);
//            }
//       
//        }
//    }];
//}

@end
